﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using EONFULLSIN.Languages;
using EONFULLSIN.Utils;

namespace EONFULLSIN.Views.Manual
{
    public partial class AdamSubControl : UserControl
    {
        private string iComPort;
        private Queue<ConcurrentDictionary<int, bool>> queueInstance;
        private bool[] adamAValue;
        private bool[] adamBValue;

        public AdamSubControl(
            string channel,
            string iCom,
            Queue<ConcurrentDictionary<int, bool>> queueIns
        )
        {
            InitializeComponent();
            groupBox3.Text = channel;
            this.txtModule.Text = iCom;
            this.queueInstance = queueIns;
            this.iComPort = iCom;
            btnCh0.Click += btnChannel_Click;
            btnCh1.Click += btnChannel_Click;
            btnCh2.Click += btnChannel_Click;
            btnCh3.Click += btnChannel_Click;
            btnCh4.Click += btnChannel_Click;
            btnCh5.Click += btnChannel_Click;
            btnCh6.Click += btnChannel_Click;
            btnCh7.Click += btnChannel_Click;
            LanguageChangedEvent.LanguageChanged += UpdateLanguage;
            // AdamHelper.AnalogEvent += Adam_AnalogEvent;
            UpdateLanguage(LanguageHelper.CurrentLanguage);
            Task.Run(() =>
            {
                while (true)
                {
                    Task.Delay(1000).Wait();
                    if (this.IsHandleCreated)
                    {
                        this.Invoke(
                            new Action(() =>
                            {
                                updateView();
                            })
                        );
                    }
                }
            });
            updateView();
        }

        private void updateView()
        {
            if (iComPort == "COM1")
            {
                adamAValue = Program.adamA.getData();
               
                if (adamAValue != null)
                {
                    txtCh0.Text = adamAValue[0] + "";
                    txtCh1.Text = adamAValue[1] + "";
                    txtCh2.Text = adamAValue[2] + "";
                    txtCh3.Text = adamAValue[3] + "";
                    txtCh4.Text = adamAValue[4] + "";
                    txtCh5.Text = adamAValue[5] + "";
                    txtCh6.Text = adamAValue[6] + "";
                    txtCh7.Text = adamAValue[7] + "";
                }
            }
            else
            {
                adamBValue = Program.adamB.getData();
                if (adamBValue != null)
                {
                    txtCh0.Text = adamBValue[0] + "";
                    txtCh1.Text = adamBValue[1] + "";
                    txtCh2.Text = adamBValue[2] + "";
                    txtCh3.Text = adamBValue[3] + "";
                    txtCh4.Text = adamBValue[4] + "";
                    txtCh5.Text = adamBValue[5] + "";
                    txtCh6.Text = adamBValue[6] + "";
                    txtCh7.Text = adamBValue[7] + "";
                }
            }
        }

        private void UpdateLanguage(string obj)
        {
            label6.Text = LanguageHelper.GetTranslation(obj, "AdamSubControl_Module");
            label5.Text = LanguageHelper.GetTranslation(obj, "AdamSubControl_ReadCount");
        }

        //private void Adam_AnalogEvent(ConcurrentDictionary<string, bool> obj)
        //{
        //    this.BeginInvoke(new Action(async () =>
        //    {

        //        foreach (var item in obj)
        //        {
        //            if (item.Key.Contains(iComPort/*"COM1"*/))
        //            {
        //                string channel = item.Key.Substring(5, 1);
        //                //txtAdamLog.BeginInvoke(new Action(() =>
        //                //{
        //                //    txtAdamLog.AppendText($"{item.Key}:{item.Value}\r\n");
        //                //}));
        //                switch (channel)
        //                {
        //                    case "0":
        //                        txtCh0.Text = item.Value.ToString();
        //                        break;
        //                    case "1":
        //                        txtCh1.Text = item.Value.ToString();
        //                        break;
        //                    case "2":
        //                        txtCh2.Text = item.Value.ToString();
        //                        break;
        //                    case "3":
        //                        txtCh3.Text = item.Value.ToString();
        //                        break;
        //                    case "4":
        //                        txtCh4.Text = item.Value.ToString();
        //                        break;
        //                    case "5":
        //                        txtCh5.Text = item.Value.ToString();
        //                        break;
        //                    case "6":
        //                        txtCh6.Text = item.Value.ToString();
        //                        break;
        //                    case "7":
        //                        txtCh7.Text = item.Value.ToString();
        //                        break;
        //                }
        //                //await Task.Delay(1000);
        //            }
        //        }
        //    }));
        //}


        private void btnChannel_Click(object sender, EventArgs e)
        {
            string tempName = ((System.Windows.Forms.Button)sender).Name.ToString();
            if (tempName.Length == 6)
                tempName = tempName.Substring(5, 1);
            else
                tempName = tempName.Substring(5, 2);
            TextBox txtBox = (TextBox)(this.Controls.Find("txtCh" + tempName, true)[0]);
            bool res = txtBox.Text == "False";
            if (iComPort == "COM1")
            {
                Program.adamA.TriggerDIO(int.Parse(tempName), res);
            }
            else
            {
                Program.adamB.TriggerDIO(int.Parse(tempName), res);
            }

            ///*AdamHelper.m_triggerQueue*/
            //ConcurrentDictionary<int, bool> pairs = new ConcurrentDictionary<int, bool>();
            //TextBox txtBox = (TextBox)(this.Controls.Find("txtCh" + tempName, true)[0]);
            //bool res = txtBox.Text == "False";
            //pairs.TryAdd(Convert.ToInt32(tempName), res);
            ////AdamTaskQueueHelper.SharedQueueIns_A.Enqueue(pairs);
            //queueInstance.Enqueue(pairs);
        }
    }
}
